﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;
using SSPharma.Biz.NhanSu;
using SSPharma.Model;
using SSPharma.Command;
using System.Windows.Input;

namespace SSPharma.ViewModel.NhanSu
{
    public class MainNhanSuViewModel : ViewModelBase
    {		
        #region Fields

        private ObservableCollection<MenuNhanSuViewModel> _MenuNhanSu;
        private RelayCommand cmdAdd;
        private RelayCommand cmdRefresh;
        private object _currentMenuViewModel;
        private RelayCommand cmdHienThiForm;
        public string _FormName;
        //private Form m_frm_CurrentForm = null
//        public ObservableCollection<Person> Persons { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand RefreshCommand
        {
            get
            {
                if (cmdRefresh == null)
                {
                    cmdRefresh = new RelayCommand(
                        param => this.Refresh(param));
                }
                return cmdRefresh;
            }
        }
        public ICommand AddCommand
        {
            get
            {
                if (cmdAdd == null)
                {
                    cmdAdd = new RelayCommand(
                        param => this.Add(param));
                }
                return cmdAdd;
            }
        }

        public void Refresh(object sender)
        {
            Load_DanhMuc();
        }
        public void Add(object sender)
        {
            string a = "";
        }
        #endregion

        #region Constructor
        public MainNhanSuViewModel()
        {
            _MenuNhanSu = new ObservableCollection<MenuNhanSuViewModel>();
            Load_DanhMuc();
            _currentMenuViewModel = new SSPharma.View.HeThong.Desktop();
        }
        #endregion

        #region Properties

        public ObservableCollection<MenuNhanSuViewModel> MenuNhanSu
        {
            get
            {
                return _MenuNhanSu;
            }
            set
            {
                _MenuNhanSu = value;
                base.OnPropertyChanged("MenuNhanSu");
            }
        }

        public object CurrentMenuViewModel
        {
            get
            {
                return _currentMenuViewModel;
            }
            set
            {
                try
                {
                    _currentMenuViewModel = value;
                    base.OnPropertyChanged("CurrentMenuViewModel");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public string FormName
        {
            get
            {
                return _FormName;
            }
            set
            {
                if (value == _FormName)
                    return;
                _FormName = value;
                base.OnPropertyChanged("FormName");
            }
        }
        #endregion

        #region Private Methods

        private void Load_DanhMuc()
        {
            /* Lay danh sach danh muc va menu*/
            BizDSNhanSu _BizDSNhanSu = new BizDSNhanSu();
            List<MenuNhanSu> lsDanhMuc_Menu = _BizDSNhanSu.DSDanhMuc_Menu();

            MenuNhanSu rootMenuNhanSu = new SSPharma.Model.MenuNhanSu();
            rootMenuNhanSu.DepartName = "Danh Mục";
            rootMenuNhanSu.DepartId = BizDSNhanSu.ROOT_MENU;

            MenuNhanSuViewModel rootMenuNhanSuModel = new MenuNhanSuViewModel(rootMenuNhanSu, this);

            for (int i = 0; i < lsDanhMuc_Menu.Count; i++)
            {
                MenuNhanSuViewModel MenuNhanSuModel = rootMenuNhanSuModel.GetMenuNhanSuModelByID(lsDanhMuc_Menu[i].ParentId);

                if (MenuNhanSuModel != null)
                {
                    MenuNhanSuModel.ChildMenuCollection.Add(new MenuNhanSuViewModel(lsDanhMuc_Menu[i], this));
                }
            }

            MenuNhanSu = rootMenuNhanSuModel.ChildMenuCollection;
        }
        //public string GetMenuNhanSuModelByID(string ParentId)
        //{
        //    var list = DbEntity.TB_Roles.Where(x => x.ParentId == parentid && x.SiteId == SiteId).ToList();
        //    if (list.Count == 0)
        //        return "";
        //    else
        //    {
        //        result += "<ul id=\"" + list[0].IdHtmlul + "\" class=\"" + list[0].ClassCssul + "\">" + list[0].BeginHtmlul;
        //        foreach (var item in list)
        //        {
        //            result += "<li id=\"" + item.IdHtmlli + "\" class=\"" + item.ClassCssli + "\">" + item.BeginHtmlli + item.RoleName + item.EndHtmlli;
        //            result += loadmenu(item.ID, site);
        //            result += "</li>";
        //        }
        //        result += "</ul>";
        //    }
        //    return result;
        //}
        #endregion

    }

}
